/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 数组;

import org.junit.Test;

public class ArrayTest {
    /**
     * @description 二分法查找
     *              前提是有序
     *
     *              秒啊
     * @author 俊昭
     * @date 2022/4/15
     */
    @Test
    public void t6(){
        int[] arr1 = new int[]{1,2,3,4,5,6,7,8,9,10};

        int dest = 8;
        boolean flag =true;
        int head = 0;
        int end = arr1.length;

        while(head<=end){
            int mid = (head + end)/2;

            if (dest == arr1[mid]){
                System.out.println(mid);
                flag=false;
                break;
            }else if (dest>arr1[mid]){
                head = mid + 1;
            }else if (dest<arr1[mid]){
                end = mid - 1;
            }
        }
        if (flag) {
            System.out.println(-1);
        }
    }

    /**
     * @description 线性查找
     * @author 俊昭
     * @date 2022/4/15
     */
    @Test
    public void t5(){
        int[] arr1 = new int[]{3,4,6,8,2,3,1,1,3,4,5,7,2,2};

        int dest = 9;

        int flag = -1;
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i]==dest){
                System.out.println(i);// 找到返回索引
                flag++;
            }
        }
        if (flag==-1){
            System.out.println(flag);
        }

    }
    /**
    * @description 数组反转
    * @author 俊昭
    * @date 2022/4/15
    */
    @Test
    public void t4(){
        int[] arr1 = new int[]{1,2,3,4,5,6,7,8,9,10};
        for (int a:arr1){
            System.out.print(a+" ");
        }
        int len = arr1.length;
        System.out.println();
        for (int i = 0; i < len/2; i++) {
            int temp = arr1[i];
            arr1[i] = arr1[len-1-i];
            arr1[len-1-i] = temp;
        }
        for (int a:arr1){
            System.out.print(a+" ");
        }
    }
    /**
     * @description 数组复制。
     * @author 俊昭
     * @date 2022/4/15
     */
    @Test
    public void t3(){
        int[] arr1 = new int[]{3,4,6,8,2,3,1,1,3,4,5,7,2,2};
        for (int a:arr1){
            System.out.print(a+" ");
        }
        System.out.println();
        int[] arr2 = new int[arr1.length];
        for (int i = 0; i < arr2.length; i++) {
            arr2[i] = arr1[i];
        }
        for (int a:arr2){
            System.out.print(a+" ");
        }

    }
    /**
     * @description 生成随机数 不能重复。
     * @author 俊昭
     * @date 2022/4/15
    */
    @Test
    public void t2(){
        int[] arr = new int[6];

        for (int i = 0; i < arr.length; i++) {

            outer:while(true){
                int temp = (int)(Math.random()*(30-1+1)+1);
                arr[i] = temp;
                for (int x = 0;x<=i;x++){
                    if (x == i){
                        break outer;

                    }else if (arr[x] == temp){
                        continue outer;
                    }
                }
            }// 不写注释可能就看不懂了?!

        }

        for (int x :arr){
            System.out.println(x);
        }
    }
    @Test
    public void t1(){
        int[][] arr = new int[][]{{3,5,8},{12,9},{7,0,6,4}};
        int sum =0;
        for (int[] i:arr){
            for (int x:i){
                System.out.print(x+" ");
                sum+=x;
            }
            System.out.println();
        }
//        System.out.println(sum);

    }
    /**
     * @description 杨辉三角
     * @author 俊昭
     * @date 2022/4/15
    */
    @Test
    public void YangHuiTriangle(){
        int layerNumber = 5;
        int[][] yanghui = new int[layerNumber][];
        for (int i = 0; i < layerNumber; i++) {
            int[] layer = new int[i+1];
            for (int j = 0;j<layer.length;j++){
                if (j==0|j==layer.length-1){
                    layer[j] = 1;
                }else{
                    layer[j] = yanghui[i-1][j-1]+yanghui[i-1][j];
                }
            }

            yanghui[i] = layer;

        }
        for (int[] i: yanghui){
            for (int x:i){
                System.out.print(x+" ");
            }
            System.out.println();
        }


    }
}
